﻿namespace com.stifel.SharePoint.Linq.SymbolExtensions
{
    using System;
    using System.Linq.Expressions;
    using System.Reflection;

    using Microsoft.SharePoint.Linq;

    public static class SymbolExtensions
    {
        #region Methods

        public static string GetSharePointLinqColumnAttributeName<T, R>( this T obj, Expression<Func<T, R>> expr )
        {
            string s = ( ( MemberExpression ) expr.Body ).Member.Name;
            MemberInfo info = typeof( T ).GetProperty( s );
            ColumnAttribute[ ] a = ( ColumnAttribute[ ] ) info.GetCustomAttributes( typeof( ColumnAttribute ), true );
            if ( a.Length > 0 )
                return a[ 0 ].Name;
            else
                return string.Empty;
        }

        #endregion Methods
    }
}